// pages/shopping_cart/shopping_cart.js
// 语言包
var chi = require('../../utils/Chinese.js')
var eng = require("../../utils/English.js")
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    // 语言包对象
    content: '',
    product: [{ id: '0', url: '../../img/tui_03.jpg', name: '焦糖味糖浆 意大利原装', price: '127', capacity: '1000ml', select: "circle", num: 1, }, { id: '0', url: '../../img/tui_03.jpg', name: '焦糖味糖浆 意大利原装', price: '127', capacity: '1000ml', select: "circle", num: 1, }],
    allSelect: "circle",
    num: 0,
    count: 0,
    show: true
  },
  // 购物车逻辑实现
  // 选择
  //改变选框状态
  change: function (e) {
    var that = this

    //得到下标
    var index = e.currentTarget.dataset.index
    //得到选中状态
    var select = e.currentTarget.dataset.select
    var newList = that.data.product
    if (select == "circle") {
      var stype = "success"
    } else {
      var stype = "circle"
    }
    //把新的值给新的数组
    var newList = that.data.product
    newList[index].select = stype
    //把新的数组传给前台
    that.setData({
      product: newList
    })
    that.judge()
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()

  },
  // 判断是否全选
  judge: function () {
    var that = this
    // 获取选中状态的select 
    var newList = that.data.product
    var len = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == 'success') {
        len++
      }

    }
    if (len == newList.length) {
      that.setData({
        allSelect: 'success'
      })
    } else {
      that.setData({
        allSelect: 'circle'
      })
    }
  },
  // 全选
  //全选
  allSelect: function (e) {
    var that = this
    //先判断现在选中没
    var allSelect = e.currentTarget.dataset.select
    var newList = that.data.product
    if (allSelect == "circle") {
      //先把数组遍历一遍，然后改掉select值
      for (var i = 0; i < newList.length; i++) {
        newList[i].select = "success"
      }
      var select = "success"
    } else {
      //取消全选
      for (var i = 0; i < newList.length; i++) {
        newList[i].select = "circle"
      }
      var select = "circle"
    }
    that.setData({
      product: newList,
      allSelect: select
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //加法
  addtion: function (e) {
    console.log(e)
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //默认99件最多
    if (num < 100) {
      num++
    }
    console.log(num)
    //把新的值给新的数组
    var newList = that.data.product
    newList[index].num = num

    //把新的数组传给前台
    that.setData({
      product: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //减法
  subtraction: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到点击的值
    var num = e.currentTarget.dataset.num
    //把新的值给新的数组
    var newList = that.data.product
    //当1件时，点击移除
    if (num == 1) {
      var lastLanuage = that.data.language
      if (lastLanuage == 'zh_CN') {
        wx.showToast({
          title: '最少购买一件哦！',
          icon: 'none',
          duration: 2000
        })
      } else if (lastLanuage == 'zh_EN') {
        wx.showToast({
          title: 'Buy at least one!',
          icon: 'none',
          duration: 2000
        })

      }
      if (that.data.product.length == '0') {
        that.setData({
          show: true
        })
      } else {
        that.setData({
          show: false
        })
      }
    } else {
      num--
      newList[index].num = num
    }

    //把新的数组传给前台
    that.setData({
      product: newList
    })
    //调用计算数目方法
    that.countNum()
    //计算金额
    that.count()
  },
  //计算数量
  countNum: function () {
    var that = this
    //遍历数组，把既选中的num加起来
    var newList = that.data.product
    var allNum = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == "success") {
        allNum += parseInt(newList[i].num)
      }
    }
    parseInt
    that.setData({
      num: allNum
    })
  },
  //计算金额方法
  count: function () {
    var that = this
    //思路和上面一致
    //选中的订单，数量*价格加起来
    var newList = that.data.product
    var newCount = 0
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == "success") {
        newCount += newList[i].num * newList[i].price
      }
    }
    that.setData({
      count: newCount
    })
  },
  // 删除
  del: function (e) {
    var that = this
    //得到下标
    var index = e.currentTarget.dataset.index
    var newList = that.data.product
    var lastLanuage = that.data.language
    var show = that.data.show
    if (lastLanuage == 'zh_CN') {
      wx.showModal({
        title: '提示',
        content: '确定删除该商品吗？',
        cancelText: "取消", //默认是“取消”
        confirmText: "确定", //默认是“确定”
        success: function (res) {
          if (res.confirm) {
            newList.splice(index, 1)
            // 判断数组长度
            if (newList.length == 0) {
              show = true
            } else {
              show = false
            }
            //把新的数组传给前台

            that.setData({
              product: newList,
              show: show
            })

          } else if (res.cancel) {
          }
        }
      })
    } else if (lastLanuage == 'zh_EN') {
      wx.showModal({
        title: 'Tips',
        content: 'Are you sure you want to delete this product?',
        cancelText: "NO", //默认是“取消”
        confirmText: "YES", //默认是“确定”
        success: function (res) {
          if (res.confirm) {
            newList.splice(index, 1)
            // 判断数组长度
            if (newList.length == 0) {
              show = true
            } else {
              show = false
            }
            //把新的数组传给前台
            that.setData({
              product: newList,
              show: show
            })
          } else if (res.cancel) {
          }
        }
      })
    }

  },
  // 跳转路径
  products_all: function () {
    wx.redirectTo({
      url: '../products_all/products_all',
    })
  },
  order: function () {
    var that = this
    var len = 0
    //遍历数组，把既选中的num加起来
    var newList = that.data.product
    for (var i = 0; i < newList.length; i++) {
      if (newList[i].select == "success") {
        len = len + 1
      }
      console.log(len)
    }
    if (len == 0) {
      var lastLanuage = that.data.language
      if (lastLanuage == 'zh_CN') {
        wx.showToast({
          title: '您还没有选择商品',
          icon: 'none',
          duration: 2000
        })
      } else if (lastLanuage == 'zh_EN') {
        wx.showToast({
          title: "You haven't selected a product yet",
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      wx.navigateTo({
        url: '../order_infor/order_infor',
      })

    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({
      language: app.data.language,
    })
    var lastLanuage = that.data.language
    if (lastLanuage == 'zh_CN') {
      that.setData({
        content: chi.content,
      })
      wx.setNavigationBarTitle({
        title: '购物车'
      })
    } else if (lastLanuage == 'zh_EN') {
      that.setData({
        content: eng.content,
      })
      // 修改导航标题
      wx.setNavigationBarTitle({
        title: 'Shopping Cart'
      })
    }
    if (that.data.product.length == '0') {
      that.setData({
        show: true
      })
    } else {
      that.setData({
        show: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    this.setData({
      language: app.data.language,
    })
    var lastLanuage = that.data.language
    if (lastLanuage == 'zh_CN') {
      that.setData({
        content: chi.content,
      })
      wx.setNavigationBarTitle({
        title: '购物车'
      })
    } else if (lastLanuage == 'zh_EN') {
      console.log('yingwen')
      that.setData({
        content: eng.content,
      })
      // 修改导航标题
      wx.setNavigationBarTitle({
        title: 'Shopping Cart'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})